package com.jgsu.sell.controller;

import com.jgsu.sell.dataobject.ProductCategory;
import com.jgsu.sell.dataobject.ProductInfo;
import com.jgsu.sell.service.CategoryService;
import com.jgsu.sell.service.ProductService;
import com.jgsu.sell.utils.ResultVoUtil;
import com.jgsu.sell.vo.ResultVO;
import com.jgsu.sell.vo.product.ProductInfoVO;
import com.jgsu.sell.vo.product.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述:
 * 买家商品端
 *
 * @author lqd12
 * @create 2018-08-16 22:35
 */
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public ResultVO list(){
        ResultVO resultVO  = new ResultVO();
        //获取上架商品
        List<ProductInfo> productInfoList = productService.findUpAll();
        //获取所有上架商品的类目
        List<Integer> categoryTypeList = new ArrayList<>();
        for(ProductInfo productInfo:productInfoList){
            categoryTypeList.add(productInfo.getCategoryType());
        }
        List<ProductCategory> productCategoryTypeList =
                categoryService.findByCategoryTypeIn(categoryTypeList);
        //拼接数据
        List<ProductVO> productVOList = new ArrayList<>();
        for(ProductCategory productCategory:productCategoryTypeList){
            ProductVO productVO = new ProductVO();
            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            productVO.setCategoryName(productCategory.getCategoryName());
            productVO.setCategoryType(productCategory.getCategoryType());
            for(ProductInfo productInfo :productInfoList){
                if(productInfo.getCategoryType().equals(productCategory.getCategoryType())){
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo,productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }
        return ResultVoUtil.success(productVOList);
    }
}